package logic

import (
	"context"
	"strings"

	"mall/common/modelx"
	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetUserListLogic {
	return &BackendGetUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BackendGetUserListLogic) BackendGetUserList(in *backend.UserListRequest) (*backend.UserListResponse, error) {
	logx.Infof("UserListRequest : %+v ", in)

	var orderBys []string
	if len(in.OrderBy) > 0 {
		for _, orderBy := range in.OrderBy {
			if orderBy.Field == "id" {
				var order string = " ASC"
				if orderBy.Order == "1" {
					order = " DESC"
				}
				orderBys = append(orderBys, model.BackendUserColumns.ID+order)
			}
		}
	}
	var stati string
	if len(in.FilterBy) > 0 {
		for _, filterBy := range in.FilterBy {
			if filterBy.Field == "status" {
				stati = filterBy.In
			}
		}
	}
	userList, count, err := l.svcCtx.BackendUser.GetList(l.ctx, model.M{
		model.BackendUserColumns.Username: in.Username,
		model.BackendUserColumns.Nickname: in.Nickname,
		model.BackendUserColumns.Name:     in.Name,
		model.BackendUserColumns.Email:    in.Email,
		model.BackendUserColumns.Mobile:   in.Mobile,
		model.BackendUserColumns.Status:   in.Status,
		"page":                            in.Current,
		"pageSize":                        in.PageSize,
		"ids":                             in.UserIds,
		"stati":                           stati,
		"orderBy":                         strings.Join(orderBys, ","),
	}, 1)
	if err != nil {
		return nil, err
	}

	var users []*backend.UserListItem
	if len(userList) > 0 {
		var currentAuthority string
		for _, user := range userList {
			if user.IsSuper > 0 {
				currentAuthority = "superAdmin"
			} else {
				currentAuthority = "admin"
			}
			users = append(users, &backend.UserListItem{
				Id:        int64(user.ID),
				Username:  user.Username,
				Name:      user.Name,
				Nickname:  user.Nickname,
				Profile:   user.Profile,
				Signature: user.Signature,
				Avatar:    user.Avatar,
				Gender:    int32(user.Gender),
				Birthday:  modelx.FormatNullDotTime(user.Birthday, ""),
				// Age:              user.Birthday,
				// DeptId:           user.DeptId,
				Group: user.Group,
				Title: user.Title,
				// Tags:             tags,
				Country: user.Country,
				Address: user.Address,
				// Geographic:       geographic,
				Email:            user.Email,
				Mobile:           user.Mobile,
				Phone:            user.Phone,
				Status:           int32(user.Status),
				DelStatus:        int32(user.DelStatus),
				CurrentAuthority: currentAuthority,
				CreatedBy:        int64(user.CreatedBy),
				CreatedAt:        user.CreatedAt.Format("2006-01-02 15:04:05"),
				UpdatedBy:        int64(user.LastUpdatedBy),
				UpdatedAt:        user.UpdatedAt.Format("2006-01-02 15:04:05"),
				// RoleId:           user.RoleId,
				// RoleName:         user.RoleName,
			})
		}
	}

	return &backend.UserListResponse{
		Total: count,
		List:  users,
	}, nil
}
